<?php


namespace app\api\controller;

use \app\common\business\User as UserBus;
use app\common\lib\Show;

class User extends AuthBase
{
    /**
     * 获取用户信息接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index() {
        //获取正常的用户数据
        $user = (new UserBus())->getNormalUserById($this->userId);
        $resultUser = [
            'id'=> $this->userId,
            'username'=> $user['username'],
            'sex'=> $user['sex']
        ];
        return show(config('status.success'), 'OK', $resultUser);
    }

    public function update() {
        if (!$this->request->isPost()) {
            return show(config('status.error', '非法请求'));
        }
        $username = $this->request->param('username', '', 'trim');
        $sex = $this->request->param('sex', 0, 'intval');
        $data = [
            'username'=> $username,
            'sex'=> $sex
        ];
        $validate= (new \app\api\validate\User())->scene('update_user');
        if (!$validate->check($data)) {
            return show(config('status.error'), $validate->getError());
        }
        $result = (new UserBus)->update($this->userId, $data);
        if ($result) {
            return show(config('status.success'), 'OK');
        }
        return show(config('status.error'), '用户信息修改失败');
    }

//    public function checkToken() {
//        if ($this->isLogin) {
//            return Show::success();
//        }
//    }
}